package com.huluobo.a2203monthdemo.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2203monthdemo.R
import com.huluobo.a2203monthdemo.aapter.RepairAdapter
import com.huluobo.a2203monthdemo.aapter.RepairUserAdapter
import com.huluobo.a2203monthdemo.databinding.ActivityRepairBinding
import com.huluobo.base.bean.RepairBean
import com.huluobo.base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairViewModel>() {
    private lateinit var repairAdapter: RepairAdapter
    private lateinit var repairUserAdapter: RepairUserAdapter
    private var isAllSelected: Boolean = false
    private val repairUserList = mutableListOf("张师傅", "李师傅", "刘师傅")
    override fun initData() {
        viewModel.getAllRepair()

        viewModel.getAllRepairSuccess.observe(this) {
            val list = Gson().fromJson(it, RepairBean::class.java)
            repairAdapter.data.clear()
            repairAdapter.data.addAll(list)
            repairAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.repairHeader.headerEditTv.visibility = View.VISIBLE
        binding.repairHeader.headerTitle.text = "维修列表"
        binding.repairHeader.headerBack.setOnClickListener { finish() }

        binding.repairHeader.headerEditTv.setOnClickListener {
            if (TextUtils.equals(binding.repairHeader.headerEditTv.text, "编辑")) {
                binding.repairHeader.headerEditTv.text = "完成"
                //切换所有item的isShow为true
                showSelect(true)
                binding.repairBottomCl.visibility = View.VISIBLE
                repairUserAdapter.data.addAll(repairUserList)
            } else {
                binding.repairHeader.headerEditTv.text = "编辑"
                showSelect(false)
                binding.repairBottomCl.visibility = View.GONE
                repairUserAdapter.data.clear()
            }
        }

        binding.repairRv.layoutManager = LinearLayoutManager(this)
        repairAdapter = RepairAdapter()
        binding.repairRv.adapter = repairAdapter

        binding.repairUserRv.layoutManager = LinearLayoutManager(this)
        repairUserAdapter = RepairUserAdapter()
        binding.repairUserRv.adapter = repairUserAdapter

        repairUserAdapter.setOnItemClickListener { adapter, view, position ->
            (adapter.getViewByPosition(
                position,
                R.id.item_repair_user_tv
            ) as TextView).setBackgroundResource(R.drawable.shape_repair_user)
        }

        binding.reairUserBtn.setOnClickListener {
            binding.repairBottomCl.visibility = View.GONE
            ToastUtils.showLong("已制定师傅维修")
        }

        binding.repairSelectAllImg.setOnClickListener {
            if (isAllSelected) {
                isAllSelected = false
                isSelect(false)
                binding.repairSelectAllImg.setImageResource(R.drawable.gou_default)
            } else {
                isAllSelected = true
                isSelect(true)
                binding.repairSelectAllImg.setImageResource(R.drawable.gou)
            }
        }
    }

    private fun showSelect(isShow: Boolean) {
        repairAdapter.data.forEach {
            it.isShow = isShow
        }
        repairAdapter.notifyDataSetChanged()
    }

    private fun isSelect(isSelected: Boolean) {
        repairAdapter.data.forEach {
            it.isSelected = isSelected
        }
        repairAdapter.notifyDataSetChanged()
    }
}